package com.gang.ssm.bos.system.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gang.ssm.bos.common.ActivityIdBean;

/**
 * 流程实例
 * @author Administrator
 *
 */
@RequestMapping("/processInstance")
@Controller
public class ProcessInstanceController {
	
	@Autowired
	private RuntimeService runtimeService;
	
	@Autowired
	private RepositoryService repositoryService;

	@RequestMapping("/list")
	public String list(Map<String, Object> map) {
		ProcessInstanceQuery query = runtimeService.createProcessInstanceQuery();
		query.orderByProcessDefinitionId().desc();
		List<ProcessInstance> list = query.list();
		
		map.put("list", list);
		//每一个流程实例的业务数据
		Map<String, String> dataMap = new HashMap<String, String>();
		for (ProcessInstance processInstance : list) {
			dataMap.put(processInstance.getId(), runtimeService.getVariables(processInstance.getId()).toString());
		}
		map.put("dataMap", dataMap);
		
		return "system/workflow/processinstance";
	}
	
	@RequestMapping("/showPng/{id}")
	public String showPng(@PathVariable("id")String id,Map<String, Object> map) {
		ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(id).singleResult();
		//2、根据流程实例对象查询流程定义id
		String processDefinitionId = processInstance.getProcessDefinitionId();
		//3、根据流程定义id查询流程定义对象
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
		//4、根据流程定义对象查询部署id
		String deploymentId = processDefinition.getDeploymentId();
		String imageName = processDefinition.getDiagramResourceName();
		
		//查询坐标
		//1、获得当前流程实例执行到哪个节点
		String activityId = processInstance.getActivityId();//usertask1
		//2、加载bpmn（xml）文件，获得一个流程定义对象
		ProcessDefinitionEntity pd = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(processDefinitionId);
		//3、根据activitiId获取含有坐标信息的对象
		ActivityImpl findActivity = pd.findActivity(activityId);
		int x = findActivity.getX();
		int y = findActivity.getY();
		int width = findActivity.getWidth();
		int height = findActivity.getHeight();
		
		ActivityIdBean activityIdBean = new ActivityIdBean(deploymentId, imageName, x, y, height, width);
		map.put("activityIdBean", activityIdBean);
		
		return "system/workflow/image";
	}
	
	@RequestMapping("/findData")
	public void findData(@RequestParam("id")String id,HttpServletResponse response) throws IOException {
		Map<String,Object> variables = runtimeService.getVariables(id);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(variables.toString());
	}
	
	@RequestMapping("/viewImage")
	public void viewImage(@RequestParam("deploymentId")String deploymentId,@RequestParam("imageName") String imageName,
				HttpServletResponse response) throws IOException {
		
		InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, imageName);
		byte[] fileDate = FileCopyUtils.copyToByteArray(inputStream);
		response.setContentType("image/png");
		response.getOutputStream().write(fileDate);
	}
}
